# -*- coding: utf-8 -*-
"""
Created on Wed Sep  7 18:50:42 2016

@author: CloudCross
"""
def be_num(nums):
    res = 0
    for i in nums:
        res = 10*res + i
    return res

from itertools import permutations
lists = [2,3,5,7,11,13,17]
res_sum = 0
a = permutations(range(10))
for i in a:
    if i[0]==0:
        continue
    else:
        is_false = False
        for j in range(2,9):
            if (i[j-1]*100+i[j]*10+i[j+1])%lists[j-2]==0:
                continue
            else:
                is_false = True
                break
        if not is_false:
            res_sum += be_num(i)
        else:
            continue
print(res_sum)